/*
 * Copyright 2019 Zhang XueHai
 * you may not use this file except in compliance with the License.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zhangxuehai.android.pdfviewer.annotation.pen;

import android.graphics.Canvas;
import android.graphics.RectF;

import com.zhangxuehai.android.pdfviewer.CoordinateUtils;
import com.zhangxuehai.android.pdfviewer.PDFView;
import com.shockwave.pdfium.util.SizeF;

import java.util.ArrayList;
import java.util.List;

/**
 * 画笔
 * @author 章学海
 * @date 2019/12/26
 */
public interface Pen<Data> extends Cloneable{
    /**画画笔注释
     * @param data 数据
     * @param canvas 画布 大小为display坐标系中，pdf的大小
     * @param scale 当前pdf 于 display 比例
     * @param basePenWidth 基础线宽，基于pdf坐标
     * @param pdfView pdf控件
     * @param page 页码*/
    void draw(Data data, Canvas canvas, float scale, int basePenWidth, PDFView pdfView,int page);

    PenType getPenType();

    void init();

    /**
     * 标注的笔，下划线，选中，删除线等
     * @author 章学海
     * @date 2019/12/31
     */
    interface MarkPen extends Pen<List<RectF>> {
        /**在同一行的矩形合并为同一个*/
        default List<RectF> mergeRect(List<RectF> data,PDFView pdfView,int page){
            RectF indexLine = null;
            List<RectF> datas = new ArrayList<>();
            if(data == null || data.size() == 0){
                return datas;
            }
            for (int i = 0; i < data.size(); i++) {

                RectF f = CoordinateUtils.toPdfPointCoordinateDesc(pdfView,page,data.get(i));
                if(f.isEmpty()){
                    continue;
                }
                if(indexLine == null){
                    indexLine = new RectF(f.left,f.top,f.right,f.bottom);
                    datas.add(indexLine);
                    continue;
                }
                //判断矩形是否属于同一行
                if(indexLine.bottom < f.top){
                    //下一行
                    indexLine = new RectF(f.left,f.top,f.right,f.bottom);
                    datas.add(indexLine);
                }else{
                    indexLine.left = Math.min(indexLine.left,f.left);
                    indexLine.top = Math.min(indexLine.top,f.top);
                    indexLine.right = Math.max(indexLine.right,f.right);
                    indexLine.bottom = Math.max(indexLine.bottom,f.bottom);
                }
            }
            return datas;
        }
    }

    /**
     * 写注释的笔
     * @author 章学海
     * @date 2019/12/31
     */
    interface WritePen extends Pen<List<SizeF>> { }
}
